using SqlSugar;
using IM.Easy.Core.Audit;
using IM.Easy.Entity.Gen;
using IM.Easy.Entity.Log;

namespace IM.Easy.Entity.Scheduled
{
    /// <summary>
    /// 调度任务表
    /// </summary>
    [SugarTable("ScheduledJob")]
    public class ScheduledJob : AuditableGuidEntity, IActive
    {
        /// <summary>
        /// 任务名称
        /// </summary>
        [SugarColumn(ColumnName = "Name", Length = 50)]
        public string Name { get; set; }

        /// <summary>
        /// 任务描述
        /// </summary>
        [SugarColumn(ColumnName = "Description", Length = 500)]
        public string? Description { get; set; }

        /// <summary>
        /// 执行次数
        /// </summary>
        [SugarColumn(ColumnName = "FireCount")]
        public int FireCount { get; set; }

        /// <summary>
        /// 下次执行时间
        /// </summary>
        [SugarColumn(ColumnName = "NextFireTime")]
        public DateTime? NextFireTime { get; set; }

        /// <summary>
        /// 上次执行时间
        /// </summary>
        [SugarColumn(ColumnName = "LastFireTime")]
        public DateTime? LastFireTime { get; set; }

        /// <summary>
        /// 任务状态
        /// </summary>
        [SugarColumn(ColumnName = "Status")]
        public SchedulerStatus Status { get; set; }

        /// <summary>
        /// 任务是否启用（true：启用，false：禁用）
        /// </summary>
        [SugarColumn(ColumnName = "IsActive")]
        public bool IsActive { get; set; }

        /// <summary>
        /// 调度任务扩展表
        /// </summary>
        [Navigate(NavigateType.OneToOne, nameof(Id), nameof(ScheduledJobExtend.JobId))]
        public ScheduledJobExtend Extend { get; set; }

        /// <summary>
        /// 触发器----先采用一对一的方式   
        /// </summary>
        [Navigate(NavigateType.OneToOne, nameof(Id), nameof(JobTrigger.JobId))]
        public JobTrigger Trigger { get; set; }

        /// <summary>
        /// 触发器
        /// </summary>
        [Navigate(NavigateType.OneToMany, nameof(JobTrigger.JobId))]
        public List<JobTrigger> Triggers { get; set; }

    }


    /// <summary>
    /// 计划任务状态
    /// </summary>
    public enum SchedulerStatus
    {
        /// <summary>
        /// 待启动（任务已创建，但尚未开始运行）
        /// </summary>
        Pending = 0,

        /// <summary>
        /// 正常（等待下一次计划作业）
        /// </summary>
        Normal = 1,

        /// <summary>
        /// 运行中（任务正在执行）
        /// </summary>
        Running = 2,

        /// <summary>
        /// 已暂停（任务已被暂停，等待恢复）
        /// </summary>
        Paused = 3,

        /// <summary>
        /// 已阻塞（任务正在等待某个事件或资源）
        /// </summary>
        Blocked = 4,

        /// <summary>
        /// 已取消（任务在执行前或执行过程中被取消）
        /// </summary>
        Canceled = 5,

        /// <summary>
        /// 已完成（任务已成功完成）
        /// </summary>
        Completed = 200,

        /// <summary>
        /// 已失败（任务执行过程中发生错误）
        /// </summary>
        Failed = -1

    }
}
